/**
 * React Static Boilerplate
 * https://github.com/kriasoft/react-static-boilerplate
 *
 * Copyright © 2015-present Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */

import React, { PropTypes } from 'react';
import history from '../../pages/history';

class Link extends React.Component {

  static propTypes = {
    to: PropTypes
      .oneOfType([PropTypes.string, PropTypes.object])
      .isRequired,
    onClick: PropTypes.func,
  };

  handleClick = (event) => {
    if (this.props.onClick) {
      this
        .props
        .onClick(event);
    }

    if (event.button !== 0/* left click */) {
      return;
    }

    if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) {
      return;
    }

    if (event.defaultPrevented === true) {
      return;
    }
    
    event.preventDefault();

    if (this.props.to && this.props.to.indexOf('http') === 0) {
      location.href = this.props.to;
    } else if (this.props.to) {
      // history.push(this.props.to);
      location.href = this.props.to;
    } else if (this.props.to !== '') {
      history.push({ pathname: event.currentTarget.pathname, search: event.currentTarget.search });
    }
  };

  render() {
    const {
      to,
      ...props
    } = this.props; // eslint-disable-line no-use-before-define
    // eslint-disable-next-line jsx-a11y/anchor-has-content
    return (<a
      href={typeof to === 'string'
      ? to
      : history.createHref(to)}
      {...props}
      onClick={this.handleClick}
    />);
  }

}

export default Link;
